import request, { initBaseUrl, getBaseUrl } from './request';
import type { IObject } from './types';

/**
 * 设置接口请求 url
 * @param baseURL url
 */
function setBaseUrl(baseURL: string) {
  initBaseUrl(baseURL);
}

function createUser(corpId: string, userName: string, password?: string, nickName?: string) {
  return request({
    url: '/visitor/create',
    method: 'get',
    params: { corpId, userName, password, nickName }
  });
}

/**
 * 用户登录，无用户先创建用户，创建好用户后，再登录获取 token
 * @param params
 * @returns
 */
function userLogin(params: IObject) {
  return request({
    url: `/oauth/token`,
    method: 'post',
    params: {
      username: params.userName,
      password: params.password,
      grant_type: 'password',
      scope: 'all'
    },
    auth: {
      username: 'client',
      password: 'secret'
    }
  });
}

/**
 * 获取工作组的配置信息
 * @param workGroupName 工作组
 */
function getWorkGroupConfig(workGroupName: string) {
  return request({
    url: `/workGroup/detail?workGroupName=${workGroupName}`,
    method: 'get'
  });
}

/**
 * 获取底部链接信息
 * @param workGroupName 工作组
 */
function getQuickLink(workGroupName: string) {
  return request({
    url: `/quickLink/getByWg?workGroupName=${workGroupName}`,
    method: 'get'
  });
}

/**
 * 获取信息栏配置
 * @param workGroupName 工作组
 */
function getInfoBar(workGroupName: string, fromClient: string) {
  return request({
    url: '/infoBar/details',
    method: 'get',
    params: {
      fromClient,
      workGroupName
    }
  });
}

/**
 * 获取访客端点评配置
 * @param workGroupName 工作组
 */
function rateConfig(workGroupName: string) {
  return request({
    url: `/rateConfig/getByWorkGroup?workGroupName=${workGroupName}`,
    method: 'get'
  });
}

/**
 * 检查是否有进行中的会话
 * @param corpId  企业id
 */
function checkExistSession(workGroupName: string) {
  return request({
    url: `/chatSession/haveSession?workGroupName=${workGroupName}`,
    method: 'get'
  });
}

/**
 * 获取企业配置信息
 * @param corpId  企业id
 */
function getCorpConfig(corpId: String) {
  return request({
    url: `/corp/getClientByCorpId?corpId=${corpId}`,
    method: 'get'
  });
}

/**
 * 获取渠道配置信息
 * @param channelId  渠道id
 * @param corpId  企业id
 */
function getChanelConfig(channelId: String, corpId: string) {
  return request({
    url: `/im/channel/getChannelInfo?channelId=${channelId}&corpId=${corpId}`,
    method: 'get'
  });
}

/**
 * 获取公告消息
 * @param workGroupName  工作组
 */
function getNotice(workGroupName: String) {
  return request({
    url: `/clientNotification/available?workGroupName=${workGroupName}`,
    method: 'get'
  });
}

/**
 * 获取离线消息
 * @param workGroupName  工作组
 */
function getOfflineMsg(workGroupName: string) {
  return request({
    url: `/message/offline?workGroupName=${workGroupName}`,
    method: 'get'
  });
}

/**
 * 获取历史记录
 * @param isHistory  是否查询历史会话
 * @param chatSessionId  当前会话ID，如不查询历史消息时，仅查询当前会话ID对应聊天内容
 * @param fromClient    渠道
 * @param workGroupName    工作组
 * @param firstMessageCreateTime    上翻翻页第一条消息时间
 * @param size    每页条数
 * @param current 当前页码
 */
function getChatRecords(querys: IObject) {
  return request({
    url: '/message/listByVisitorAndWorkGroup',
    method: 'get',
    params: querys
  });
}

/**
 * 访客请求接入
 * @param workgroupName  工作组
 */
function requestWorkgroup(
  workgroupName: string,
  alongData: string,
  fromClient = 'web',
  robotId?: string,
  accessTrackId?: string,
  inviteId?: string
) {
  return request({
    url: '/chatSession/workgroup',
    method: 'get',
    params: {
      workgroupName,
      alongData,
      inviteId,
      accessTrackId,
      fromClient,
      robotSessionId: robotId
    }
  });
}
/**
 * 保存机器人-提问消息(如：关键字消息)
 *
 * @param workgroupName
 * @param id
 * @param type 类型， 4： 关键词
 * @param question
 * @param sid
 */
function saveRobotQuestion(sid: string, question: string, type: string) {
  return request({
    url: '/message/saveRobotQuestion',
    params: {
      type: type,
      question: question,
      chatSessionId: sid
    },
    method: 'get'
  });
}

/**
 * 请求机器人
 * @param work  渠道id
 */
function requestRobot(workGroupName: string, alongData: string, fromClient: string) {
  return request({
    url: `/assistant/root?workGroupName=${workGroupName}&alongData=${alongData}&fromClient=${fromClient}`,
    method: 'get'
  });
}

/**
 * 分组查询在线助手
 * @param querys  查询参数
 * -- workGroupName 工作组id
 * -- parentId  问题父id
 * -- page 当前页码
 */
function getCommonQuestion(querys: IObject) {
  return request({
    url: '/assistant/commonQuestion',
    method: 'get',
    params: querys
  });
}

/**
 * 获取在线助手回答
 *
 * @param workGroupName
 * @param id
 * @param question
 * @param chatSessionId
 */
function getRobotAnswer(
  workGroupName: string,
  id: string,
  question: string,
  chatSessionId: string
) {
  return request({
    url: '/assistant/answer',
    method: 'get',
    params: {
      workGroupName,
      id,
      question,
      chatSessionId
    }
  });
}

/**
 * 消息发送
 * @param data  消息内容
 */
function sendMessage(data: IObject) {
  return request({
    url: '/message/send',
    method: 'post',
    data
  });
}

/**
 * 消息撤回
 * @param messageId  消息id
 */
function withDrawMessage(messageId: string) {
  return request({
    url: `/message/withDraw?messageId=${messageId}`,
    method: 'get'
  });
}

/**
 * 满意度评价
 * @param querys 请求参数
 *
 * rate {
 * -- chatSessionId 会话id
 * -- rate5Type 评价类型 1：系统自动发起评价 2：坐席主动邀请评价 3:访客主动发起评价
 * -- rate 评价星级
 * -- rating 评价内容
 * -- comment 评价内容
 * -- tags 评价标签
 * -- fromClient
 * }
 */
function sendRate(querys: IObject) {
  return request({
    url: '/chatSession/rate',
    method: 'post',
    params: querys
  });
}

/**
 * 发送留言
 * @param data  请求参数
 * @param data.mobile
 * @param data.email
 * @param data.content
 * @param data.workgroupName
 */

function sendleaveMessage(data: IObject) {
  return request({
    url: '/leave/message/insert',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    },
    data: objectToFormData(data)
  });
}

/**
 * 查询会话是否已评价
 * @param chatSessionId  会话 id
 */
function isRate(chatSessionId: string) {
  return request({
    url: `/chatSession/isRated?chatSessionId=${chatSessionId}`,
    method: 'get'
  });
}

/**
 * 将普通对象转换为 formData 对象
 * @param obj 对象
 * @param formData  formData 对象
 * @returns formData
 */
function objectToFormData(obj: IObject, formData = new FormData()) {
  // 遍历对象的所有可枚举属性
  for (const [key, value] of Object.entries(obj)) {
    formData.append(key, value);
  }
  return formData;
}

/**
 * 上传文件
 * @param { object}  data-参数
 * @param { string}  data.file-文件
 * @param { string}  data.workgroupName-工作组名称
 * @param { string}  data.chatSessionId-会话id
 */
function uploadFile(data: IObject) {
  data.client = 'visitor';
  return request({
    url: '/message/upload',
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data: objectToFormData(data)
  });
}

/**
 * 结束会话
 * @param chatSessionId 会话 id
 */
function closeChat(chatSessionId: string) {
  return request({
    url: `/chatSession/visitor/close?chatSessionId=${chatSessionId}`,
    method: 'get'
  });
}

/**
 * 获取坐席信息
 * @param username 坐席账号
 * @returns
 */
function getAgentInfo(username: string) {
  return request({
    url: `/user/baseInfo?username=${username}`,
    method: 'get'
  });
}
/**
 * 请求视频客服
 * @param data 请求数据
 */
function videoChat(data: any) {
  return request({
    url: '/cti/video/visitor/appoint',
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data: objectToFormData(data)
  });
}

/**
 * 请求指定客服进线
 * @param workgroupName 工作组
 * @param agentName 坐席名称
 */
function appointAgent({
  workgroupName,
  agentName,
  alongData,
  inviteId,
  accessTrackId,
  fromClient,
  robotId
}: {
  workgroupName: string;
  agentName: string;
  alongData: string;
  fromClient: string;
  robotId?: string;
  accessTrackId?: string;
  inviteId?: string;
}) {
  return request({
    url: '/chatSession/appoint',
    method: 'get',
    params: {
      workgroupName,
      agentName,
      alongData,
      inviteId,
      accessTrackId,
      fromClient,
      robotSessionId: robotId
    }
  });
}

export default {
  setBaseUrl,
  getBaseUrl,
  createUser,
  userLogin,
  checkExistSession,
  getCorpConfig,
  getChanelConfig,
  rateConfig,
  getWorkGroupConfig,
  getQuickLink,
  getInfoBar,
  getNotice,
  getOfflineMsg,
  getChatRecords,
  requestWorkgroup,
  requestRobot,
  sendMessage,
  withDrawMessage,
  sendRate,
  sendleaveMessage,
  getRobotAnswer,
  saveRobotQuestion,
  isRate,
  getCommonQuestion,
  uploadFile,
  closeChat,
  getAgentInfo,
  videoChat,
  appointAgent
};
